<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
             label-width="80px">
      <el-form-item label="菜单级别" prop="type">
        <el-radio-group v-model="dataForm.type">
          <el-radio v-for="(type, index) in typeList" :label="index" :key="index">{{ type }}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="餐厅名称" prop="resType" v-if="dataForm.type === 0">
        <el-input v-model="dataForm.resName" placeholder="餐厅名称"/>
      </el-form-item>
      <el-form-item label="上级菜单" prop="parentId" v-if="dataForm.type === 1">
        <el-select v-model="dataForm.parentId" placeholder="所属楼/楼层">
          <el-option v-for="item in superior" :key="item.value" :label="item.label" :value="item.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="就餐类型" prop="eatType" v-if="dataForm.type === 1">
        <el-input v-model="dataForm.eatName" placeholder="就餐类型名称"/>
      </el-form-item>
<!--
      <el-form-item label="折扣比例" prop="discount" v-if="dataForm.type === 1">
        <el-input-number v-model="dataForm.discount" controls-position="right" :min="1" :max="10" placeholder="折扣比例">
        </el-input-number>
      </el-form-item>
-->
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" :loading="this.isLoading">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        isLoading: false,
        typeList: ['餐厅', '折扣'],
        dataForm: {
          id: 0,
          type: 0,
          parentId: '',
          eatName: '',
          resName: '',
          discount: '',
          createTime: '',
          isDel: ''
        },
        dataRule: {
          resName: [
            {required: true, message: '餐厅不能为空', trigger: 'blur'}
          ],
          eatName: [
            {required: true, message: '就餐类型不能为空', trigger: 'blur'}
          ],
          discount: [
            {required: false, message: '折扣比例不能为空', trigger: 'blur'}
          ],
          createTime: [
            {required: false, message: '创建时间不能为空', trigger: 'blur'}
          ],
          isDel: [
            {required: false, message: '逻辑删除不能为空', trigger: 'blur'}
          ]
        }
      }
    },
    props: {
      superior: Array
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.isLoading = false
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          this.resetFrom()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/tra/resdiscount/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                console.log(data.resDiscount)
                this.dataForm.parentId = data.resDiscount.parentId
                this.dataForm.type = data.resDiscount.type
                this.dataForm.resName = data.resDiscount.resName
                this.dataForm.eatName = data.resDiscount.eatName
                this.dataForm.createTime = data.resDiscount.createTime
                this.dataForm.isDel = data.resDiscount.isDel
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.isLoadingVisible()
            this.$http({
              url: this.$http.adornUrl(`/tra/resdiscount/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'resName': this.dataForm.resName,
                'eatName': this.dataForm.eatName,
                'parentId': this.dataForm.parentId,
                'createTime': this.dataForm.createTime,
                'isDel': this.dataForm.isDel
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.isLoadingVisible()
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
                this.isLoadingVisible()
              }
            })
          }
        })
      },
      isLoadingVisible () {
        this.isLoading = !this.isLoading
      },
      resetFrom () {
        this.dataForm.type = 0
        this.dataForm.parentId = ''
        this.dataForm.resName = ''
        this.dataForm.eatName = ''
        this.dataForm.discount = ''
        this.dataForm.isDelete = ''
        this.dataForm.createTime = ''
      }
    }
  }
</script>
